// miniprogram/pages/user/index/index.js
const app = getApp()
const db=wx.cloud.database()

Page({

  /**
   * 页面的初始数据
   */
  data: {
    official:true,
    userInfo: {},
    logged: false,
    UserInfo:{
      avatarUrl: '',
      userName:'点击头像进行登录',
      School:"",
      Major:"",
      XueYuan:"",
      manager:"",
      Prize:"",
      Grade:"",
      Introduction:"",
      BiaoQian:[]
    },
    publish:{
      Interest:[],
      FindTeam:[],
      FindTeammates:[]
    },
    logged:false

    
  },

  /**
   * 生命周期函数--监听页面加载
   */

  
  onLoad: function (options) {
    
    this.checkUser()
    // 获取用户信息

   
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
   

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  
  checkUser:function(e)
  {
    var that = this
    console.log(app.globalData.openid)
    db.collection('user').where({
      _openid:'app.globalData.openid' // 填入当前用户 openid
    }).get({
      success:res=>{ 
        console.log(res)
        if(res.data.length)
        {
        that.setData({
            logged:true,
            UserInfo:res.data[0].UserInfo,
            publish:res.data[0].publish
          })
         
        
        } 
        else{
          wx.getSetting({ 
            success: res => { 
              if (res.authSetting['scope.userInfo']) { 
                // 已经授权，可以直接调用 getUserInfo 获取头像昵称，不会弹框 
                wx.getUserInfo({ 
                  success: res => { 
                    this.setData({ 
                      ['UserInfo.avatarUrl']: res.userInfo.avatarUrl, 
                      ['UserInfo.userName']:res.userInfo.nickName ,
                      logged:true      
                    },()=>{
                      db.collection('user').add({
                        data:{
                          UserInfo:this.data.UserInfo,
                          publish:this.data.publish
                        },
                        success(res){
                          console.log(yes)
                        }
                      })
                    }
                    )
                   
                  } 
                }) 
              } 
            } 
          }) 
        }
       
      },
      fail(err){
        console.log(err)
        
      }
    })
   
  },
  gotoeditUser:function(e)
  {
    const that = this
    wx.navigateTo({
      url: '../editUserInfo/editUserInfo',
      events: {
        // 为指定事件添加一个监听器，获取被打开页面传送到当前页面的数据
      },
      success: function(res) {
        // 通过eventChannel向被打开页面传送数据
        res.eventChannel.emit('acceptDataFromOpenerPage', { data: that.data.UserInfo})
      }
    })
  },
  gotoUser:function(){
    const that = this
    wx.navigateTo({
      url: '../userInfo/userInfo',
      events: {
        // 为指定事件添加一个监听器，获取被打开页面传送到当前页面的数据
      },
      success: function(res) {
        // 通过eventChannel向被打开页面传送数据
        res.eventChannel.emit('acceptDataFromOpenerPage', { data: that.data.UserInfo})
      }
    })
  },
  bindGetUserInfo: function(e) {
    console.log(e.detail.userInfo)
    if (e.detail.userInfo){
      this.setData({
        logged:true,
        ['UserInfo.userName']:e.detail.userInfo.nickName,
        ['UserInfo.avatarUrl']: e.detail.userInfo.avatarUrl,
      },()=>{
        db.collection('user').add({
        data:{
          UserInfo:this.data.UserInfo,
          publish:this.data.publish
        },
        success(res){
          console.log(yes)
        }
      })
      }
      )

      

    } else {
      //用户按了拒绝按钮
    }
  },
  

  gotointerest:function()
  {
    const that = this 
    wx.navigateTo({
      url: '../interest/interest',
      events: {
        // 为指定事件添加一个监听器，获取被打开页面传送到当前页面的数据
      },
      success: function(res) {
        // 通过eventChannel向被打开页面传送数据
        res.eventChannel.emit('acceptDataFromOpenerPage', { data: that.data.publish})
      }
    })
  },
  gotohistory:function()
  {
    const that = this
    wx.navigateTo({
      url: '../history/index/index',
      events: {
        // 为指定事件添加一个监听器，获取被打开页面传送到当前页面的数据
      },
      success: function(res) {
        // 通过eventChannel向被打开页面传送数据
        res.eventChannel.emit('acceptDataFromOpenerPage', { data: that.data.publish})
      }
    })
  },
  gotomypublish:function(){

    app.globalData.TeamIndex = 3;
    wx.switchTab({
      url: '../../Team/index/index',
      
    })
  }
  
})